package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface SetmealDao {
    void add(Setmeal setmeal);

    @Insert("insert into t_setmeal_checkgroup values(#{setmeal_id},#{checkgroup_id})")
    void setSetmealAndCheckGroup(HashMap<String, Integer> map);

    Page<Setmeal> selectByCondition(String queryString);

    @Select("select * from t_setmeal where id = #{id}")
    Setmeal findById(Integer id);

    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{id}")
    List<Integer> findCheckGroupIdsBySetMealId(Integer id);

    @Delete("delete from t_setmeal_checkgroup where setmeal_id = #{id}")
    void deleteAssociation(Integer id);

    void update(Setmeal setmeal);

    @Select("select count(*) from t_setmeal_checkgroup where setmeal_id = #{id}")
    Long SelectCountBySetMealId(Integer id);

    @Delete("delete from t_setmeal where id = #{id}")
    void delete(Integer id);

    @Select("select * from t_setmeal")
    List<Setmeal> findAll();

    @Select("select * from t_setmeal where id = #{id} ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "code", column = "code"),
            @Result(property = "helpCode", column = "helpCode"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "age", column = "age"),
            @Result(property = "price", column = "price"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "img", column = "img"),
            @Result(
                    column = "id",
                    property = "checkGroups",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckGroupDao.findCheckGroupById")
            )
    })
    Setmeal findBySetMealId(Integer id);

    @Select("select s.name,count(o.id) value from t_order o,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();
}
